﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using ImageLib.Data;
using System.IO;
using ImageLib.Data.Database;

namespace ImageLib.Shell.Model
{
    internal class MainModel
    {
        internal static class CommandKey
        {
            public const string AddLibrary = "AddLibrary";
            public const string GetCurrentLibraryImages = "GetCurrentLibraryImages";
            public const string GetCurrentFolderImages = "GetCurrentFolderImages";
            public const string ShowSmallIcons = "Small Icons";
            public const string ShowLargeIcons = "Large Icons";
            public const string ShowTiles = "Tiles";
            public const string ShowContent = "Content";
        }
        
        #region Data
        private DataModel _dataModel;
        #endregion

        /// <summary>
        /// Initializes a new instance of the MainModel class.
        /// </summary>
        public MainModel()
        {
            _dataModel = new DataModel();
            CurrentFolder = string.Empty;
        }

        public Library CurrentLibrary { get; private set; }

        public string CurrentFolder { get; set; }

        public IList<string> Folders { get; private set; }

        public string ImageListType { get; set; }

        public bool ShowAllImages { get; set; }

        internal string CurrentDirPath
        {
            get
            {
                if (CurrentLibrary == null || string.IsNullOrEmpty(CurrentLibrary.Path)) { return null; }
                if (string.IsNullOrEmpty(CurrentFolder)) { return CurrentLibrary.Path; }

                string result = string.Format("{0}{1}",CurrentLibrary.Path, CurrentFolder);
                return result;
            }
        }

        public IList<Library> GetLibraries()
        {
            return _dataModel.GetLibraries().OrderBy((lib) => lib.Name).ToList();
        }

        public void SetCurrentLibrary(Guid libraryId)
        {            
            CurrentLibrary = _dataModel.GetLibrary(libraryId);
            Folders = _dataModel.GetSubFolders(libraryId).ToList();            
        }
                
        public Guid AddLibrary(string libraryPath)
        {
            if (string.IsNullOrEmpty(libraryPath)) { return Guid.Empty; }

            var libraryName = Path.GetFileName(libraryPath);

            Guid libraryId = _dataModel.AddLibrary(libraryName, libraryPath);

            AddLibraryImages(libraryPath, libraryId); 

            return libraryId;
        }

        public IList<Image> GetCurrentLibraryImages()
        {
            return _dataModel.GetImages(CurrentLibrary.Id).ToList();
        }

        public IList<Image> GetCurrentFolderImages()
        {
            return _dataModel.GetImages(CurrentLibrary.Id, CurrentFolder).ToList();
        }

        private void AddLibraryImages(string libraryPath, Guid libraryId)
        {
            const string imageFilter = "*.jpg|*.png|*.bmp";

            var images = imageFilter.Split('|').SelectMany(filter => Directory.GetFiles(libraryPath, filter, SearchOption.AllDirectories)).ToList();

            foreach (var img in images)
            {
                var name = Path.GetFileNameWithoutExtension(img);
                var extension = Path.GetExtension(img);
                var relativePath = Path.GetDirectoryName(img).Remove(0, libraryPath.Length);
                var created = File.GetCreationTime(img);

                _dataModel.AddImage(name, relativePath, extension, created, libraryId);
            }
        }
        #region Static methods
        
        public static string SelectDirectory()
        {
            string result = null;

            using (FolderBrowserDialog folderDialog = new FolderBrowserDialog())
            {
                if (folderDialog.ShowDialog() == DialogResult.OK)
                {
                    result = folderDialog.SelectedPath;
                }
            }

            return result;
        }

        public static string GetDirPath(string libraryPath, string folderPath)
        {
            if (string.IsNullOrEmpty(libraryPath)) { return null; }

            string currentDirPath = string.IsNullOrEmpty(folderPath) ? libraryPath
                                                                                       : string.Format("{0}{1}", libraryPath, folderPath);
            return currentDirPath;
        }
        #endregion
    }
}
